package com.servlet.checking;

import com.domain.Checking;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.service.CheckingService;
import com.service.impl.CheckingServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/checkingSelectServlet")
public class CheckingSelectServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        CheckingService service = new CheckingServiceImpl();
        ArrayList<Checking> ck = service.select();
        ObjectMapper mapper = new ObjectMapper();
        StringBuilder darte = new StringBuilder();
        if(ck.size() < 1) {
            response.getWriter().write("{\"status\":\"0\",\"message\":\"ok\",\"data\":[]}");
            return;
        }
        for(Checking checking1 : ck){
            String j = mapper.writeValueAsString(checking1);
            darte.append(j);
            darte.append(",");

        }
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        darte.deleteCharAt(darte.length()-1);
        response.getWriter().write("{\"status\":\"0\",\"message\":\"ok\",\"data\":["+darte+"]}");

    }
}
